package com.liuzu.esys.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.CommentBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.listener.WebCommentListener;
import com.liuzu.esys.pojo.Comment;
import com.liuzu.esys.service.CommentService;
import com.liuzu.esys.util.OssUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/commentList")
    public LayuiPageVo<Comment> commentList(@RequestParam Map<String,Object> params){
        System.out.println(params);
        return commentService.commentPageData(params);
    }

    //添加库存信息
    @RequestMapping("/commentadd")
    public R commentAdd(Comment comment){
        int i = commentService.commentAdd(comment);
        if(i==1){
            return R.ok();
        }
        return R.error();
    }

    //修改库存信息
    @RequestMapping("/commentupdate")
    public R commentUpdate(Comment comment){
        int i = commentService.commentUpdate(comment);
        if(i==1){
            return R.ok();
        }
        return R.error();
    }

    //删除库存信息
    @RequestMapping("/commentdelete")
    public R commentDelete(int id){
        int i = commentService.commentDelete(id);
        if(i==1){
            return R.ok();
        }
        return R.error();
    }
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        if (file != null) {
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            // 将文件转为输入流
            InputStream inputStream = file.getInputStream();


            String url = OssUtil.uploadFile(inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url", url);
        }
        return R.ok();
    }
//    @Autowired
//    private WebCommentListener webCommentListener;
//
//
//    @RequestMapping("/templateDownload")
//    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//
//        String realPath = request.getServletContext().getRealPath("/template/商品信息模板.xlsx");
//
//        File file = new File(realPath);
//
//        byte[] bytes = FileUtils.readFileToByteArray(file);
//
//        String filename = URLEncoder.encode("商品信息模板","utf-8");
//
//        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
//
//        ServletOutputStream outputStream = response.getOutputStream();
//
//        outputStream.write(bytes);
//
//        outputStream.close();
//    }
//
//    @RequestMapping("/import")
//    public R importExcel(MultipartFile file) throws IOException {
//        // 避免遇见请求报错
//        if(file!=null){
//            /**
//             * 参数1  文件流
//             * 参数2  读取到的数据封装到哪个数据类型中
//             * 参数3  监听器  每读取一行数据 就会调用监听器中的 invoke  方法
//             * */
//            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), CommentBo.class, webCommentListener);
//            // 得到 sheet 对象
//            ExcelReaderSheetBuilder sheet = read.sheet();
//            // 读取sheet
//            sheet.doRead();
//
//        }
//        return R.ok();
//    }
//
//    @RequestMapping("/export")
//    public void export(Comment comment,HttpServletResponse response) throws IOException {
//        //下载excel响应数据格式
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        // 下载的文件名中文要转码  不然会出现乱码
//        String filename = URLEncoder.encode("商品信息导出", "utf-8");
//
//        // 下载文件
//        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
//
//        //得到指向浏览器的输出流  向浏览器写输出
//        ServletOutputStream outputStream = response.getOutputStream();
//
//        ExcelWriterBuilder write = EasyExcel.write(outputStream, CommentBo.class);
//
//        ExcelWriterSheetBuilder sheet = write.sheet();
//        //准备满足条件的集合数据
//        List<CommentBo> comments = commentService.commentExcelData(comment);
//        // 把这个集合数据 写到 输出流
//        sheet.doWrite(comments);
//    }
}
